﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CRUDServiceProvider.cs" company="DissingNellemann">
//   
// All rights reserved </copyright>
// <summary>
//   The crud service provider.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Website.Models
{
    using System;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.Web;

    using Website.CRUDServices;

    using User = Website.Models.Domain.User;

    /// <summary>
    ///     The crud service provider.
    /// </summary>
    public static class CRUDServiceProvider
    {
        #region Public Methods and Operators

        /// <summary>
        /// The with crud service.
        /// </summary>
        /// <param name="action">
        /// The action.
        /// </param>
        /// <exception cref="Exception">
        /// May throw an exception.
        /// </exception>
        public static void WithCRUDService(Action<CRUDServicesClient> action)
        {
            using (var client = new CRUDServicesClient())
            using (var scope = new OperationContextScope(client.InnerChannel))
            {
                User loggedInUser = null;
                if (HttpContext.Current.Session["LoggedInUser"] != null)
                {
                    loggedInUser = (User)HttpContext.Current.Session["LoggedInUser"];
                }

                if (loggedInUser == null)
                {
                    throw new Exception("Du er ikke logget ind.");
                }

                var userName = new MessageHeader<string>(loggedInUser.Username);
                MessageHeader userNameHeader = userName.GetUntypedHeader("UserName", "ns");
                OperationContext.Current.OutgoingMessageHeaders.Add(userNameHeader);

                var password = new MessageHeader<string>(loggedInUser.Password);
                MessageHeader passwordHeader = password.GetUntypedHeader("UserPassword", "ns");
                OperationContext.Current.OutgoingMessageHeaders.Add(passwordHeader);

                var userID = new MessageHeader<string>(loggedInUser.ID.ToString());
                MessageHeader userIDHeader = userID.GetUntypedHeader("UserID", "ns");
                OperationContext.Current.OutgoingMessageHeaders.Add(userIDHeader);

                action(client);

                client.Close();
            }
        }

        /// <summary>
        /// The with crud service.
        /// </summary>
        /// <param name="action">
        /// The action.
        /// </param>
        /// <typeparam name="T">
        /// The return type of the action performed.
        /// </typeparam>
        /// <returns>
        /// The <see cref="T"/>.
        /// </returns>
        public static T WithCRUDService<T>(Func<CRUDServicesClient, T> action)
        {
            T result = default(T);

            WithCRUDService(client => { result = action(client); });

            return result;
        }

        #endregion
    }
}